package algorithm_optimal_solution.LinkedList;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

//合并两个有序的单链表：时间复杂度O(N+M)。将头节点值大的链表插入到较小链表中，注意判断最后谁为空。
public class MergeTwoLinkedListAsc {

    static class Node{
        private int value;
        private Node node;

        public Node(int value,Node node){
            this.value = value;
            this.node = node;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public Node getNode() {
            return node;
        }

        public void setNode(Node node){
            this.node = node;
        }
    }

    public void merge(Node head1,Node head2){
        Node cur1 = head1.getValue() < head2.getValue() ? head1 : head2;
        Node head = cur1;
        Node cur2 = cur1 == head1 ? head2 : head1;
        Node pre = null;
        Node next = null;
        while(cur1 != null && cur2 != null){
            if(cur1.getValue() < cur2.getValue()){
                pre = cur1;
                cur1 = cur1.getNode();
            }else{
                next = cur2.getNode();
                pre.setNode(cur2);
                cur2.setNode(cur1);
                pre = cur2;
                cur2 = next;
            }
        }
        if(cur1 == null){
            pre.setNode(cur2);
        }
        StringBuilder sb = new StringBuilder();
        while(head != null){
            sb.append(head.getValue()).append(" ");
            head = head.getNode();
        }
        System.out.print(sb);
    }

    public static void main(String[] args)throws IOException {
        BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
        int len = Integer.parseInt(scanner.readLine());
        String[] numbers = scanner.readLine().split(" ");
        Node node = null;
        for(int i = len - 1;i >= 0;i--){
            node = new Node(Integer.parseInt(numbers[i]),node);
        }
        int twoLen = Integer.parseInt(scanner.readLine());
        String[] twoNumbers = scanner.readLine().split(" ");
        Node twoNode = null;
        for(int i = twoLen - 1;i >= 0;i--){
            twoNode = new Node(Integer.parseInt(twoNumbers[i]),twoNode);
        }
        new MergeTwoLinkedListAsc().merge(node,twoNode);
    }
}
